﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineCooperation.DB;
using OnlineCooperation.Entity;

namespace OnlineCooperation
{
    public partial class ResetPwd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Email.Value=Session["UserEmail"].ToString();
            
        }

        protected void ButtonResetPwdClick(object sender, EventArgs e)
        {
            UserDB ud = new UserDB();
            User u = ud.GetUserByEmail(Session["UserEmail"].ToString());

            string name=NewName.Value;
            if (name=="")
                name = u.Name;

            string opwd=OldPwd.Value;
            string npwd=NewPwd.Value;
            string cpwd=ConfirmPwd.Value;

            if (opwd=="" && npwd == "" && cpwd == "")
                opwd = npwd = cpwd = u.Pwd;

            if (u.Pwd!= opwd)
            {
                Response.Write("<script>alert('旧密码输入不正确！')</script>");
                return;
            }

            if (npwd.Length < 6)
            {
                Response.Write("<script>alert('请输入足够长的新密码！')</script>");
                return;
            }


            if (npwd != cpwd)
            {
                Response.Write("<script>alert('两次输入的新密码不一致！')</script>");
                return;
            }
            u = new User(u.ID, name, Session["UserEmail"].ToString(), npwd, u.Icon);

            int code=ud.Update(u);

            switch (code)
            {
                case 0:
                    Response.Write("<script>alert('用户名已存在')</script>");
                    break;
                default:
                    Session["UserName"] = u.Name;
                    Response.Write("<script>alert('修改成功');window.location='HomePageJump.aspx';</script>");
                    break;
            }
        }
    }
}